// 嘉旺控制器
module.exports = app => {
    return class JiawangController extends app.Controller {
        // 添加食品种类
        async addFoodCategory() {
            let {
                foodCategory
            } = this.ctx.request.body;
            this.ctx.body = await this.app.mongo.insertMany('foodCategory',{docs: foodCategory});
        }
        
        // 添加食品
        async addFoods() {
            let foods = this.ctx.request.body.foods;
            this.ctx.body = await this.app.mongo.insertMany('foods', {docs: foods});
        }
        
        // 获取食品种类列表(全部)
        async foodList() {
            const foodCategories = await this.app.mongo.find('foodCategory');
            const foods = await this.app.mongo.find('foods');
            const arr = [];
            for (let entry of foodCategories) {
                entry.foods = foods.filter(item=> {
                    return item.category_id === entry.id
                })
                arr.push(entry);
            }
            this.ctx.body = {
                list: arr,
                code: 0,
                msg: 'success'
            }
        }
        
        async findAllFoods() {
            const result = await this.app.mongo.find('foods');
             this.ctx.body = {
                list: result,
                code: 0,
                msg: 'success'
            }
        }
        // 根据种类查找食品列表
        async findFoods() {
            let id = this.ctx.query.id;
            const result = await this.app.mongo.find('foods', {query: {category_id: Number(id)}});
             this.ctx.body = {
                list: result,
                code: 0,
                msg: 'success'
            }
        }
    };
};
